/****************************************************************************
 * 2025.8 小庆永不失眠
 ****************************************************************************/

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using QFramework;
using UnityEngine.U2D;

namespace ProjectSurvivor
{
	public partial class AchievementController : UIElement
	{
		ResLoader mResloader = ResLoader.Allocate();
		private void Awake()
		{
			var originLocalPosY = AchievementItem.LocalPositionY();
			var iconAltas = mResloader.LoadSync<SpriteAtlas>("Icon");

			AchievementSystem.OnAchievementUnlocked.Register(item=>
			{
				Title.text = $"<b>成就 {item.Name} 达成!</b>";
				Description.text = item.Description;
				Icon.sprite =  iconAltas.GetSprite(item.IconName);
				AchievementItem.Show();
				AchievementItem.LocalPositionY(-200);
				AudioKit.PlaySound("Achievement");


				ActionKit.Sequence()
				.Lerp(-200,originLocalPosY,0.3f,y=>
				{
					AchievementItem.LocalPositionY(y);
				}).Delay(2)
				.Lerp(originLocalPosY,-200,0.3f,y=>
				{
                    AchievementItem.LocalPositionY(y);
                },()=>
				{
					AchievementItem.Hide();
				}).Start(this);
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
		}

		protected override void OnBeforeDestroy()
		{
			mResloader.Recycle2Cache();
			mResloader = null;
		}
	}
}